(in-package #:experiment)

(defun start-ms-timer (ms &rest timer-args &key &allow-other-keys)
  "Return a timer structure that can be canceled with cancel-timer."
  (unless (experiment-disable-timers-p *experiment*)
    (gwee:run-with-ms-timer ms nil
                            'state-dispatch-event
                            *experiment*
                            (current-state *experiment*)
                            (apply 'make-instance 'timer-event timer-args))))

(defun start-timer (secs &rest timer-args &key &allow-other-keys)
  "Return a timer structure that can be canceled with cancel-timer."
  ;; FIXME: if this timer outlasts the state then things are gonna get
  ;; funky probably. Should we be able to specify whether it
  ;; transcends states?
  (unless (experiment-disable-timers-p *experiment*)
    (gwee:run-with-timer secs nil
                         'state-dispatch-event
                         *experiment*
                         (current-state *experiment*)
                         (apply 'make-instance 'timer-event timer-args))))

(defun start-interval (secs &rest timer-args &key &allow-other-keys)
  "Start a timer that creates timer events every SECS seconds until
canceled. Returns a timer structure that can be canceled with
cancel-timer."
  ;; FIXME: if this timer outlasts the state then things are gonna get
  ;; funky probably. Should we be able to specify whether it
  ;; transcends states?
  (unless (experiment-disable-timers-p *experiment*)
    (gwee:run-with-timer secs secs
                         'state-dispatch-event
                         *experiment*
                         (current-state *experiment*)
                         (apply 'make-instance 'timer-event timer-args))))

(defun cancel-timer (timer)
  (gwee:cancel-timer timer))

(defun cancel-state-timers (state)
  (declare (ignore state))
  ;; FIXME: dont kill them all!
  (setf gwee::*timer-list* nil))
